#encoding=utf8
from module.dimension_classifier.dimension_classifier import DimensionDetection

import module.condition_classifier.Classifier as condition_classifier
# from module.intention_classifier.intention_classifier import agg_module_interface,agg_module_interface_v2
from module.intention_classifier.intention_classifier1_new import IntentionClassfier
from utils.DataLoader import DataLoader
from utils.operation_separate import break_ques
import sys
import traceback
"""
    用于后端测试
"""
if __name__ == "__main__":
    query = ""
    metaData_path = "./metaData/data_dict_new.json"
    loader = DataLoader("./testData/queries.csv")
    data_dict = loader.loadData_csv()
    for query in data_dict:
        question = query['question']
        # 维度分类
        # dim_detect = DimensionDetection(metaData_path)
        # dim_output = dim_detect.detect(question)
        # sel = dim_output['sel']
        dim_detect = DimensionDetection(metaData_path)
        dim_output, choices, like_dict, dim_pos = dim_detect.detect(question)
        dim_output = dim_detect.output_formatter(question, dim_output)
        sel = dim_output['sel']
        print(dim_pos)
        # # 矛盾操作拆分
        # dict_list_after_break = break_ques(dim_output)
        # # 意图识别
        # intent_outputs = []
        # for dim_output_after_break in dict_list_after_break:
        #     intent_outputs.append(agg_module_interface(dim_output_after_break,dim_pos))
        
        # intent_outputs=agg_module_interface_v2(dim_output,dim_pos)
        try:
            intent_detect=IntentionClassfier(metaData_path)
            intent_outputs=intent_detect.predict(dim_output,dim_pos)
        except Exception as e:
            print(traceback.format_exc())
            break#return
        # intent_detect=IntentionClassfier(metaData_path)
        # intent_outputs=intent_detect.predict(dim_output,dim_pos)
        # 条件分类
        for intent_output in intent_outputs:
            cond_detect = condition_classifier.Classifier(metaData_path)
            final_output = cond_detect.classification(intent_output['question'], intent_output['sel'],
                                                          intent_output['agg'], intent_output['limit'],
                                                          intent_output['measure'], dim_output['like'],None)
            for output in final_output:
                cond_detect.print_sql(output)
            print(intent_output['mdx'])
        break